varying vec3 pos, norm;

	void main()
	{
		
		// turn these into uniforms
		vec3 lightDir = normalize(vec3(2,2,2));
		float specular_power = 40.0;

		float N = 1.4;
		float mf = pow( ( N - 1.0 ) / ( N + 1.0 ), 2.0) ;

		// consider doing these per vertex
		vec3 view = normalize(pos);
		vec3 pnorm = normalize(norm);
		vec3 halfV = normalize(( -view + lightDir) / 2.0);
		float ndotHalfV = max(dot( pnorm, halfV ), 0.0);

		// compute specular component
		float ks = pow(ndotHalfV, specular_power);

		// compute diffuse component
		float kd = max(dot(pnorm, lightDir), 0.0);
	
		// compute fresnel term
		float c = -abs(dot(view, pnorm));
		float kf = mf + (1.0 - mf) * pow( 1.0 + c, 3.0);

		// get the reflection color
		// procedurally what we do here is find the y component
		// of reflection vector and use it to find color
		float ry = view.y - 2.0 * dot( view, pnorm ) * pnorm.y;
		float rl = fract(-ry);
		rl *= pow(ry, 0.2); // softens the edge 0.0 = hard edge

		// multiply by colors (move colors to uniforms)
		vec3 cd = kd * vec3(0.5,0.0,0.0);
		vec3 cs = ks * vec3(0.6,0.6,0.2);
		vec3 cf = kf * rl * vec3(1.0,1.0,1.0);

		gl_FragColor = vec4(cf + cs + cd, 1.0);

	}